package com.hhsjtest.videoplayer

import android.Manifest
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.provider.Settings
import android.widget.MediaController
import android.widget.Toast
import android.widget.VideoView
import androidx.annotation.Nullable
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import java.io.File

class VideoActivity : Activity() {

    var videoView: VideoView? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.act_main)

        val videoView = findViewById<VideoView>(R.id.video_view)

        this.videoView = videoView;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            if (Environment.isExternalStorageManager()) {
                //本地地址
                var path = filePath()
                videoView.setVideoPath(path)
            } else {
                val intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
                intent.data = Uri.parse("package:$packageName");
                startActivityForResult(intent, 1);
            }
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // 先判断有没有权限
            if (ActivityCompat.checkSelfPermission(
                    this,
                    Manifest.permission.READ_EXTERNAL_STORAGE
                ) == PackageManager.PERMISSION_GRANTED &&
                ContextCompat.checkSelfPermission(
                    this,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
                ) == PackageManager.PERMISSION_GRANTED
            ) {
                ///sdcard
                //本地地址
                var path = filePath()
                videoView.setVideoPath(path)
            } else {
                ActivityCompat.requestPermissions(
                    this,
                    arrayOf(
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE
                    ),
                    1
                );
            }
        } else {
            //本地地址
            var path = filePath()
            videoView.setVideoPath(path)
        }
//
//        //网络地址
//        var path = "http://192.168.9.21:8080/video/video.mp4"
//        videoView.setVideoPath(Uri.parse(path).toString())

//        videoView.setVideoURI(Uri.parse("android.resource://"+packageName+"/"+R.raw.video))

        /**
         * MediaController
         */
        val controller = MediaController(this)
        videoView.setMediaController(controller)
        videoView.start();
        videoView.requestFocus();
//        controller.setMediaPlayer(videoView)

    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == 1) {
            if (ActivityCompat.checkSelfPermission(
                    this,
                    Manifest.permission.READ_EXTERNAL_STORAGE
                ) == PackageManager.PERMISSION_GRANTED &&
                ContextCompat.checkSelfPermission(
                    this,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
                ) == PackageManager.PERMISSION_GRANTED
            ) {
                //本地地址
                var path = filePath()
                videoView?.setVideoPath(path)
            } else {
                Toast.makeText(this, "存储权限获取失败", Toast.LENGTH_SHORT).show()
            }
        }
    }

    ///sdcard/video.mp4
    fun filePath(): String = File.separator + "sdcard" + File.separator + "video.mp4"

    override fun onActivityResult(requestCode: Int, resultCode: Int, @Nullable data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            if (Environment.isExternalStorageManager()) {
                //本地地址
                var path = filePath()
                videoView?.setVideoPath(path)
            } else {
                Toast.makeText(this, "存储权限获取失败", Toast.LENGTH_SHORT).show()
            }
        }
    }

}